package com.flqy.baselibrary.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    private static final long ONE_MINUTE = 60;
    private static final long ONE_HOUR = 3600;
    private static final long ONE_DAY = 86400;
    private static final long ONE_MONTH = 2592000;
    private static final long ONE_YEAR = 31104000;


    /**
     * "yyyy-MM-dd HH:mm:ss"
     *
     * @return
     */
    public static String getDateTime() {
        return getFormatDateTime("yyyy-MM-dd");
    }

    public static String getNextDay(String date) {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date temp = dft.parse(date);
            Calendar cld = Calendar.getInstance();
            cld.setTime(temp);
            cld.add(Calendar.DATE, 1);
            temp = cld.getTime();
            //获得下一天日期字符串
            String nextDay = dft.format(temp);
            return nextDay;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * "yyyy-MM-dd HH:mm"
     *
     * @return
     */
    public static String getDateTime(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = new Date(timeStamp);
        return sdf.format(date);
    }

    /**
     * 获取指定格式的当前时间
     *
     * @param format 时间格式
     * @return
     */
    public static String getFormatDateTime(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 获取指定格式的指定时间
     */
    public static String getFormatDateTime(long timeStamp, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date(timeStamp);
        return sdf.format(date);
    }

    public static String getChineseDateStr(long timeStamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timeStamp);
        return String.format("%d月%d日 %02d:%02d", c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
    }

    public static String getChineseDateStr2(long timeStamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timeStamp);
        return String.format("%d年%d月%d日 %02d:%02d", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
    }

    /**
     * 距离今天多久
     *
     * @param timeStamp
     * @return
     */
    public static String fromToday(long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);

        long time = timeStamp / 1000;
        long now = new Date().getTime() / 1000;
        long ago = now - time;
        if (ago <= ONE_MINUTE) {
            return ago + "s前";
        } else if (ago <= ONE_HOUR)
            return ago / ONE_MINUTE + "分钟前";
        else if (ago <= ONE_DAY)
            return ago / ONE_HOUR + "小时前";
        else if (ago <= ONE_MONTH) {
            long day = ago / ONE_DAY;
            return day + "天前";
        } else if (ago <= ONE_YEAR) {
            long month = ago / ONE_MONTH;
            return month + "个月前";
        } else {
            long year = ago / ONE_YEAR;
            return year + "年前";
        }
    }

    /**
     * 根据秒返回时分秒
     *
     * @param time
     * @return
     */
    public static String getFormatHMS(long time) {
        //time = time / 1000;//总秒数
        int s = (int) (time % 60);//秒
        int m = (int) ((time % 3600) / 60);//分
        int h = (int) (time / 3600);//时
        return String.format("%02d:%02d:%02d", h, m, s);
    }

    /**
     * 根据毫秒返回时分秒
     *
     * @param time
     * @return
     */
    public static String getFormatHMS2(long time) {
        //time = time / 1000;//总秒数
        int m = (int) ((time % 3600) / 60);//分
        int h = (int) (time / 3600);//时
        return h + "小时" + m + "分";
    }

    /**
     * 根据毫秒返回时分秒
     *
     * @param time
     * @return
     */
    public static String getFormatHMS3(long time) {
        int s = (int) (time % 60);//秒
        int m = (int) ((time % 3600) / 60);//分
        int h = (int) (time / 3600);//时
        return h + "小时" + m + "分" + s + "秒";
    }

    /**
     * 根据毫秒返回时分
     *
     * @param time
     * @return
     */
    public static String getFormatHM(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        int h = c.get(Calendar.HOUR_OF_DAY);
        int m = c.get(Calendar.MINUTE);
        return String.format("%02d:%02d", h, m);
    }

    /**
     * 判断是不是打卡时间
     *
     * @param time
     * @return
     */
    public static boolean isClockTime(boolean isMorning) {
        Calendar c = Calendar.getInstance();
        int h = c.get(Calendar.HOUR_OF_DAY);
        if (isMorning) {
            if (h > 4 && h < 13) {
                return true;
            }
        } else {
            if (h > 17 || h < 3) {
                return true;
            }
        }
        return false;
    }

    public static boolean isSameDay(long time1, long time2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTimeInMillis(time1);
        cal2.setTimeInMillis(time2);
        boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
        return sameDay;
    }
}
